import { CoreEntity, InnerMineEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { SpawnLayoutConfig } from "GameLoop/WorldState/EntityManager/Entitys/room/spawn/spawnlayout";
import { PositionHelper } from "utils/position/PositionHelper";
import { StationNetwork } from "GameLoop/Path/StationNetwork";
import { BaseMinerial, BaseMinerialConst, ZipMinerial } from "utils/constants/globalconstant";
import { hold_place } from "GameLoop/WorldState/mount/move/action.virtual";




/**
 * 中央搬运工
 */ 
 /*@profile*/
export class Manager<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     * 搬运工行为,先做简化行为。运输的两端都有container
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
        // global.Log.Info(`${arg.entity.name}`);
        const creep = arg.entity;
      
        const coreEntity = arg.entity.ownerRoom().getEntity("CoreEntity");
        StationNetwork.getInstance.PathFinder.RegisterBlockCreep(creep);
        if(coreEntity.ManagerPosition.isEqualTo(creep.pos))
        {
            hold_place(creep);
            // 1.先指定清理
            if(!coreEntity.room.memory.clearingState  ) 
            {
                // 空间不足且能量不足
                if(coreEntity.room.canExecute(151)&& coreEntity.freeSpace()<50000 && coreEntity.resourceCount(RESOURCE_ENERGY)<30000)
                {
                    coreEntity.room.memory.clearingState=1000;
                }
            }
            else 
            {
                if(coreEntity.freeSpace()>50000)
                {
                    coreEntity.room.memory.clearingState=undefined
                }
                else 
                {
                    // 清理模式
                    coreEntity.room.memory.clearingState--;

                    const action = this.clearAction(creep,coreEntity);
                    if(action)
                    {
                        return action
                    }
                }
                
            } 
            const transportTask = arg.entity.ownerRoom().getTaskByType("core")[0] ;
            // 转移能量
            
            if(!transportTask  )
            {
                // 空闲模式
                const actions = this.prepareAction(creep,coreEntity);
                if(actions)
                {
                    return actions
                }
                creep.memory.bindid = undefined
                return []
            }
            if(creep.memory.bindid)
            {
                // 任务模式
                if(transportTask.data.arg.List.length==0)
                {
                    creep.memory.bindid = undefined
                    arg.entity.ownerRoom().deleteTaskById(transportTask.id,"core");
                    creep.ownerRoom().getEntity("CoreEntity").onTransferComplete(transportTask);
                    return [];
                }
                return this.taskAction(creep,transportTask)
            }
            else 
            {
                // 空闲模式
                const actions = this.prepareAction(creep,coreEntity);
                if(actions)
                {
                    return actions
                }
                else 
                {
                    creep.memory.bindid = transportTask.id;
                }
            }
            return []
        }
        else
        {
            // StationNetwork.getInstance.PathFinder.RegisterBlockCreep(creep);
            return [ActionArgHelper.actionMoveto(creep,{pos:coreEntity.ManagerPosition,range:0},{})]
        }
    }
    private taskAction(creep:Creep,task:ITask<ICreepTaskData<"transportAccurate">>)
    {
        return [ActionArgHelper.warp(
            ActionArgHelper.actionAccurateTransport(creep,task.data.arg ),
            ()=>
            {
                creep.memory.bindid = undefined
                creep.ownerRoom().getEntity("CoreEntity").onTransferComplete(task);
                creep.ownerRoom().deleteTaskById(task.id,"core");
            }
            )]
    }

    private prepareAction(creep:Creep,coreEntity:CoreEntity)
    {
        if(creep.store.getUsedCapacity()>0)
        {
            const stores = coreEntity.getStores({resourceType:RESOURCE_ENERGY,amount:creep.store.getUsedCapacity()});
            if(stores.length>0)
            {
                return [ActionArgHelper.actionTransfer(creep,stores[0])]
            }
        }
        return undefined;
    }

    /**
     * 仓库的清理Action
     * @param creep 
     * @param coreEntity 
     */
    private clearAction(creep:Creep,coreEntity:CoreEntity)
    {
        if(creep.store.getUsedCapacity()>0)
        {
            return [ActionArgHelper.actionDrop(creep,creep.pos)]
        }
        else 
        {
            const type =  coreEntity.room.getMinerial()[0].mineralType;
            
            const getDropAction = (minertial:MineralConstant)=>
            {
                const types = [minertial,ZipMinerial[type]] as ResourceConstant[];
                for(const e of types)
                {
                    const stores = coreEntity.getTakeStores({resourceType:e,amount:creep.store.getFreeCapacity()});
                    if(stores.length>0)
                    {
                        return [ActionArgHelper.actionWithdraw(creep,stores[0],[{resourceType:e}])]
                    }
                }
                return undefined;
            }
            const acitons = getDropAction(type);
            if(acitons)
            {
                return acitons;
            }
            for(const e of BaseMinerialConst)
            {
                if(type == e)
                {
                    continue;
                }
                return getDropAction(e);
            }
        }
        return undefined;
    }
}
